/**
 * @title IntroActivity.java
 * @package com.esun.cheetah.view
 * @description 介绍页面
 * @author liugan
 * @date 2012-4-28
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.introduce;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.esun.cheetah.R;
import com.esun.cheetah.view.HomeAct;

/**
 * @title IntroActivity
 * @description 第一次运行时的介绍页面的Activity
 * @author liugan
 */
public class IntroActivity extends Activity {
	// 页面切换的控件
	private ViewPager mViewPager;
	private Button btnStart;
	private ImageView ivPageHint;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.introduce_activity);
		ivPageHint = (ImageView) findViewById(R.id.iv_introduce_page_hint);
		mViewPager = (ViewPager) findViewById(R.id.vp_introduce_viewpager);
		// 初始化4个页面
		View[] views = new View[3];
		views[0] = new ImageView(this);
		views[0].setBackgroundResource(R.drawable.p01);
		views[1] = new ImageView(this);
		views[1].setBackgroundResource(R.drawable.p02);
		views[2] = LayoutInflater.from(this).inflate(R.layout.last_splash_page,
				null);
		btnStart = (Button) views[2].findViewById(R.id.btn_start);
		mViewPager.setAdapter(new ViewAdapter(views));
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			private float lastOffset = 0.0f;
			private boolean needChange = false;

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}

			@Override
			public void onPageScrolled(int page, float positionOffset, int arg2) {

				if ((lastOffset <= 0.5 && positionOffset >= 0.5)
						|| (lastOffset >= 0.5 && positionOffset <= 0.5)) {
					needChange = true;
				}

				if (needChange) {
					if (positionOffset > 0.5) {
						ivPageHint.getDrawable().setLevel(page + 1);
					} else {
						ivPageHint.getDrawable().setLevel(page);
					}
				}

				needChange = false;
				lastOffset = positionOffset;

			}

			@Override
			public void onPageSelected(int page) {

			}

		});
		btnStart.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent().setClass(getApplicationContext(),
						HomeAct.class));
				finish();
			}
		});

	}
}
